package cn.com.nthotline.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.com.nthotline.model.SubCategory;

public class DBSubcategory implements DBInterface<SubCategory> {
	private DatabaseHelper helper;

	public DBSubcategory(Context context) {
		helper = new DatabaseHelper(context);
	}

	@Override
	public long insert(SubCategory t) {
		// TODO Auto-generated method stub
		ContentValues values = new ContentValues();
		values.put(SubCategory.CATID, t.catId);
		values.put(SubCategory.CATNAME, t.catName);
		values.put(SubCategory.DESCRPT, t.descrpt);
		values.put(SubCategory.KEYWORDS, t.keyWords);
		values.put(SubCategory.TITLE, t.title);
		return helper.insertData(CreateDbConstants.TABLE_SUBCATEGORY, values);
	}

	@Override
	public List<SubCategory> query(String id) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<SubCategory> queryCategoryById(String id) {
		List<SubCategory> list = new ArrayList<SubCategory>();
		SQLiteDatabase db = helper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "
				+ CreateDbConstants.TABLE_SUBCATEGORY + " where "
				+ SubCategory.CATID + "=?", new String[] { id });
		while (cursor.moveToNext()) {
			SubCategory category = new SubCategory();
			category.catId = cursor.getString(cursor
					.getColumnIndex(SubCategory.CATID));
			category.catName = cursor.getString(cursor
					.getColumnIndex(SubCategory.CATNAME));
			category.descrpt = cursor.getString(cursor
					.getColumnIndex(SubCategory.DESCRPT));
			category.keyWords = cursor.getString(cursor
					.getColumnIndex(SubCategory.KEYWORDS));
			category.title = cursor.getString(cursor
					.getColumnIndex(SubCategory.TITLE));
			list.add(category);
		}
		return list;
	}
}
